<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<%@ page import="com.fckbug.vos.DetailVO"%>
<%@ page import="com.fckbug.vos.ContentVO"%>
<%@ page import="com.fckbug.service.QAService"%>
<%@ page import="com.fckbug.service.UserService"%>
<%@ page import="com.fckbug.util.SpringContextHolder"%>
<%@ page import="com.fckbug.util.TimeUtil"%>
<%@ page import="java.util.List"%>
<%@ page import="com.fckbug.vos.Acc"%>
<%DetailVO detail = (DetailVO)request.getAttribute("detail"); 
QAService qaService = (QAService)SpringContextHolder.getBean("qaService");
UserService userService = (UserService)SpringContextHolder.getBean("userService");
ContentVO question = detail.getQuestion(); 
List<ContentVO> answers = detail.getAnswers();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><%=question.getTitle() %></title>
 <link rel="stylesheet" type="text/css" href="/css/all.css?v=1">
<script src="/js/jquery-1.6.1.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
function ajaxQAMark(theObj,increment){
	var p = theObj.parent();
	var id = p.find("input").val();
	var tUrl = '/user/alterat.do';
	var isQ = p.find("input[name='qid']").size()>0;
	if(isQ) {
		tUrl = '/user/alterqt.do';
	}
	var tdata = (isQ?'qid=':'aid=')+id+'&increment='+increment;
	var mark = parseInt(p.find(".vote-count-post").text());
	$.ajax({
		url: tUrl,
		type: "POST",
		dataType: "html",
		timeout: 4000,
		data: tdata,
		success: function(response){
			if(response=='success'){p.find(".vote-count-post").text(mark+increment);}
			else{alert(response);}
		},
		error: function(x, t, m) {
			//toShowWrap.html("");
	        if(t==="timeout") {
	            alert("查询超时，请重新查询");
	        } else {
	            alert(t);
	        }
	    }
	});
}
$(function(){
	CKEDITOR.replace( 'contentTextarea', {
	//extraPlugins: 'autogrow',
	//autoGrow_maxHeight: 260,
	height: '300px',
	toolbar:  [
	{ name: 'basicstyles', groups: [ 'basicstyles' ], items: [ 'Bold', 'Italic', 'Underline', 'Strike'] },
	{ name: 'list', groups: [ 'list', 'indent', 'blocks'], items: [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote']},
	{ name: 'colors', items: [ 'TextColor', 'BGColor' ] },
	{ name: 'tools', items: [ 'Maximize' ] }
	],
	// Remove the Resize plugin as it does not make sense to use it in conjunction with the AutoGrow plugin.
	removePlugins: 'elementspath'
	//resize_enabled: false
	});
	$(".vote-up-off").click(function(){
		ajaxQAMark($(this),1);
	});
	$(".vote-down-off").click(function(){
		ajaxQAMark($(this),-1);
	});
	$(".star-on").click(function(){
		var p = $(this).parent();
		var mark = parseInt(p.find(".favoritecount-selected").text());
		$.ajax({
			url: '/user/alterft.do',
			type: "POST",
			dataType: "html",
			timeout: 4000,
			data: 'qid='+p.find("input").val()+'&increment=1',
			success: function(response){
				if(response=='success'){p.find(".favoritecount-selected").text(mark+1);}
				else{alert(response);}
			},
			error: function(x, t, m) {
				//toShowWrap.html("");
		        if(t==="timeout") {
		            alert("查询超时，请重新查询");
		        } else {
		            alert(t);
		        }
		    }
		});
	});
	$("span[qaid]").click(function(){
		if($(this).parent().next().size()==0) {
		var qaId = $(this).attr("qaid");
		var type = $(this).attr("type");
		$(this).parent().after('<form action="/user/savec.do" method="post"><textarea name="content" cols="90" rows="3" style="margin: 2px; height: 48px; width: 650px;font-size:100%;"></textarea><input type="hidden" name="qaid"value="'+qaId+'"/><input type="hidden" name="type"value="'+type+'"/><input type="hidden" name="qid"value="<%=question.getId() %>"/><input type="submit" value="提交"/></form>');
		}else{
			$(this).parent().next().remove();
		}
	})
})
</script>
</head>
<body>

	<div id="custom-header"></div>
	<div class="container">
		<jsp:include page="../header.jsp"></jsp:include>
		<div id="content">
			<div id="question-header">
				<h1 itemprop="name">
					<%=question.getTitle() %>
				</h1>
			</div>
			<div id="mainbar">
				<div id="question" class="question" data-questionid="17577836">
				<table><tbody>
				<tr>
				<td class="votecell">
				<div class="vote">
					<input type="hidden" name="qid" value="<%=question.getId()%>"> 
					<a class="vote-up-off" title="This question shows research effort; it is useful and clear">up vote</a>
					<span class="vote-count-post "><%=question.getMarkTimes()%></span>
					<a class="vote-down-off" title="This question does not show any research effort; it is unclear or not useful">down vote</a>
					<a class="star-off star-on" title="This is a favorite question (click again to undo)">favorite</a>
					<div class="favoritecount"><b class="favoritecount-selected"><%=question.getFavoriteTimes()%></b></div>
				</div>
				</td>
				<td class="postcell">
				<div class="post-text" itemprop="description">
				<%=question.getContent() %>
				</div>
				<div class="fw">
					<div class="post-signature owner">
					<%Acc qacc = userService.getUserByUserId(question.getCreateUserId()); %>
						<div class="user-info ">
							<div class="user-gravatar32"><a href="/users/175086/shane"><div class=""><img src="/head/<%=qacc.getId() %>"alt="" width="32" height="32"></div></a>
							</div>
							<div class="user-details">
								<a href="/user/profile.do?id=<%=qacc.getId() %>"><%=qacc.getNickName() %></a><br> <span class="reputation-score" title="reputation score"dir="ltr">366</span>
							</div>
						</div>
					</div>
					<!-- <div class="post-menu"><a href="/posts/2648984/edit"class="suggest-edit-post"title="revise and improve this post">编辑</a></div> -->
				</div>
				</td>
				</tr>
				<tr>
					<td class="votecell"></td>
					<td>
					<%if(question.getComments().size()!=0){ %>
						<div id="comments-1182958" class="comments dno">
							<table>
								<tbody>
									<%List<ContentVO> comments = question.getComments();
									for(int i=0;i<comments.size();i++) {
										ContentVO comment = comments.get(i);
										Acc acc = userService.getUserByUserId(comment.getCreateUserId());
									%>
									<tr class="comment">
										<td></td>
										<td class="comment-text">
										<div><span class="comment-copy"><%=comment.getContent() %></span> –&nbsp;<a href="/user/profile.do?id=<%=acc.getId() %>" title="1903 reputation" class="comment-user owner"><%=acc.getNickName() %></a> <span class="comment-date" dir="ltr"><a class="comment-link" href="#comment773487_964804"><span title="2009-06-08 13:35:28Z" class="relativetime-clean"><%=TimeUtil.getNormalDate(comment.getCtTime()) %></span></a></span></div>
										</td>
									</tr>
									<%} %>
								</tbody>

							</table>
						</div>
						<%} %>
						<div><span id="comments-link-1182958" class="comments-link" data-comments-counts="0" title="" qaid="<%=question.getId() %>" type="<%=question.getType() %>"><a href="javascript:void(0)">评论</a></span></div>
					</td>
				</tr>
				</tbody></table>
				</div>
				<div id="answers">
					<div id="answers-header">
						<div class="subheader answers-subheader">
							<h2><%=answers.size() %>&nbsp;回答</h2>
						</div>
					</div>
					<%
	   				 for(int i=0;i<answers.size();i++){ContentVO a = answers.get(i);%>
					<div id="answer-17578509" class="answer" data-answerid="<%=a.getId() %>">
						<table>
							<tbody>
								<tr>
									<td class="votecell">
									<div class="vote">
										<input type="hidden" value="<%=a.getId() %>" name="aid"> <a
											class="vote-up-off" title="This answer is useful">up
											vote</a> <span class="vote-count-post "><%=qaService.getATime(a.getId()).get(0) %></span> <a
											class="vote-down-off" title="This answer is not useful">down
											vote</a>
									</div>
									</td>
									<td class="answercell">
									<div class="post-text" itemprop="description"><%=a.getContent() %></div>
									<div class="fw">
										<div class="post-signature owner">
										<%Acc acc = userService.getUserByUserId(a.getCreateUserId()); %>
											<div class="user-info ">
												<div class="user-gravatar32">
													<a href="/users/175086/shane"><div class="">
													<img src="/head/<%=acc.getId() %>" alt="" width="32" height="32"/>
													</div></a>
												</div>
												<div class="user-details">
													<a href="/user/profile.do?id=<%=acc.getId()%>"><%=acc.getNickName() %></a><br> <span class="reputation-score" title="reputation score"	dir="ltr">366</span>
												</div>
											</div>
										</div>
										<!-- <div class="post-menu"><a href="/posts/2648984/edit" class="suggest-edit-post"	title="revise and improve this post">编辑</a></div> -->
									</div>
									</td>
								</tr>
							<tr>
								<td class="votecell"></td>
								<td>
								<%if(a.getComments().size()!=0){ %>
									<div id="comments-1182958" class="comments dno">
										<table>
											<tbody>
												<%List<ContentVO> Acomments = a.getComments();
												for(int j=0;j<Acomments.size();j++) {
													ContentVO comment = Acomments.get(j);
													Acc cacc = userService.getUserByUserId(comment.getCreateUserId());
												%>
												<tr class="comment">
													<td></td>
													<td class="comment-text">
													<div><span class="comment-copy"><%=comment.getContent() %></span> –&nbsp;<a href="/user/profile.do?id=<%=cacc.getId() %>" title="1903 reputation" class="comment-user owner"><%=cacc.getNickName() %></a> <span class="comment-date" dir="ltr"><a class="comment-link" href="#comment773487_964804"><span title="2009-06-08 13:35:28Z" class="relativetime-clean"><%=TimeUtil.getNormalDate(comment.getCtTime()) %></span></a></span></div>
													</td>
												</tr>
												<%} %>
											</tbody>
			
										</table>
									</div><%} %>
									<div><span id="comments-link-1182958" class="comments-link" data-comments-counts="0" title="" qaid="<%=a.getId() %>" type="<%=a.getType()%>"><a href="javascript:void(0)">评论</a></span></div>
								</td>
							</tr>
							</tbody>
						</table>
					</div>
					<%} %>
					<form id="post-form" class="post-form" method="post"
						action="/user/savea.do">
						<input type="hidden" name="qid" value="<%=question.getId() %>"/>
						<h2 class="space">您的回答</h2>
						<div id="post-editor" class="post-editor">
						<textarea cols="80"  id="contentTextarea" name="content" style="display:none;" rows="20" class=""></textarea>
						</div>
						<input type="submit" value="回答"/>
					</form>
				</div>
				
			</div>
			<div id="sidebar" class="show-votes">
				<div class="module question-stats">
					<table id="qinfo">
						<tbody>
							<tr>
								<td><p class="label-key">创建</p></td>
								<td style="padding-left: 10px"><p class="label-key"
										title="">
										<b><%=TimeUtil.getNormalDate(question.getCtTime()) %></b>
									</p></td>
							</tr>
							<tr>
								<td><p class="label-key">阅读</p></td>
								<td style="padding-left: 10px">
									<p class="label-key">
										<b><%=question.getViewTimes() %>次</b>
									</p>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
				
			</div>
		</div>
	</div>
</body>
</html>